home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 422_02 / misc / dis85.c < prev    next >
C/C++ Source or Header  |  1994-03-20  |  12KB  |  381 lines

  1. /* 8085 CROSS DISASSEMBLER:
  2.  
  3. DIS85 Command Syntax:
  4.  
  5.     DIS85 <filename> [option switches ...] [>outfile]
  6.  
  7. <filename> is the name of the file containing the 8085 machine code to
  8. be disassembled. The file is read as pure binary data. If your file is
  9. in INTEL or MOTOROLA hex format (such as might be output by a PROM reader
  10. etc), you can use the HEXFMT command supplied with our development kits
  11. and cross assemblers to convertit.
  12.  
  13. Option Switches:
  14.  
  15. -a        Inhibits the display of Address, Opcode data, and ASCII text fields
  16.         in the output file. When this option is enabled, the resulting output
  17.         file is suitable for input to our ASM85 assembler.
  18.  
  19. -b        Similar to '-d' below, except that symbols are also generated for
  20.         8 bit (BYTE) constants.
  21.  
  22. -d        Causes DIS85 to pre-define symbols for any addresses which are used
  23.         within the memory range of the image. Symbols are defined as 'Hxxxx'
  24.         where 'xxxx' is the memory address in hexidecimal. When this option
  25.         is used, symbols are generated for all accesses. Any symbols not
  26.         occurring within the disassembly are output as 'EQU' statements.
  27.  
  28. -e        When using the 'd=' option to write the symbol table to a file, this
  29.         causes DIS85 to Exclude addresses which do not occur within the range
  30.         of memory covered by the disassembly.
  31.  
  32. a=value    Specifies the address (in hex) of the first instruction which is
  33.         disassembled. If not given, DIS85 assumes code starts at $0000.
  34.  
  35. b=size    Specify offset into file to begin disassembly. If this parameter is
  36.         given, DIS85 will skip the operand "size" bytes in the input file
  37.         before beginning disassembly.
  38.  
  39. d=file    Causes DIS85 to write its internal symbol table to the named file.
  40.         Automatically invokes the '-d' option.
  41.  
  42. n=size    Specify the number of instructions to disassemble. If given, DIS85
  43.         will only disassemble the requested "size" number of instructions.
  44.  
  45. s=file    Specify a symbol table file. Each line in the file must contain a
  46.         symbol name, followed by a value (in hex). If the value occurs
  47.         anywhere in the disassembly, DIS85 will substitute the symbol name.
  48.  
  49. Output File:
  50.  
  51. DIS85 writes its output to the DOS standard output device, which will
  52. normally appear on the PC console, but may be redirected to a file with
  53. the '>' operator. The format of each line output by DIS85 is as follows:
  54.  
  55.   <address> <opcode> <ascii> <label> <instr> <operands>
  56.  
  57. <address>    - Address of this instruction
  58. <opcode>    - HEXidecimal opcode bytes
  59. <ascii>        - ASCII/text representation of <opcode>
  60. <label>        - Any symbolic label occuring at this address
  61. <instr>        - Symbolic 8085 instruction
  62. <operands>    - Any operands to <instruction>
  63.  
  64. Copyright 1991-1994 Dave Dunfield
  65. All rights reserved.
  66.  
  67. Permission granted for personal (non-commercial) use only.
  68.  
  69. Compile command: cc dis85 -fop
  70. */
  71. #include <stdio.h>
  72.  
  73. #define    SYMBOLS    2500        /* Maximum number of symbols to allow */
  74.  
  75. #define    BIT8    0x01        /* Symbol is referenced as 8-bit */
  76. #define    BIT16    0x02        /* Symbol is referenced as 16-bit */
  77. #define    SYMDEF    0x80        /* Symbol is defined */
  78. #define    SYMOLD    0x40        /* Symbol was loaded */
  79.  
  80. /*
  81.  * Table of instruction opcodes: MASK, COMPARE, TYPE/LENGTH, TEXT
  82.  */
  83. char itable[] = {
  84.     0xFF, 0xFE, 0x02, 'C', 'P', 'I', ' ', 0,
  85.     0xFF, 0x3A, 0x03, 'L', 'D', 'A', ' ', 0,
  86.     0xFF, 0x32, 0x03, 'S', 'T', 'A', ' ', 0,
  87.     0xFF, 0x2A, 0x03, 'L', 'H', 'L', 'D', ' ', 0,
  88.     0xFF, 0x22, 0x03, 'S', 'H', 'L', 'D', ' ', 0,
  89.     0xFF, 0xF5, 0x01, 'P', 'U', 'S', 'H', ' ', 'P', 'S', 'W', 0,
  90.     0xFF, 0xF1, 0x01, 'P', 'O', 'P', ' ', 'P', 'S', 'W', 0,
  91.     0xFF, 0x27, 0x01, 'D', 'A', 'A', 0,
  92.     0xFF, 0x76, 0x01, 'H', 'L', 'T', 0,
  93.     0xFF, 0xFB, 0x01, 'E', 'I', 0,
  94.     0xFF, 0xF3, 0x01, 'D', 'I', 0,
  95.     0xFF, 0x37, 0x01, 'S', 'T', 'C', 0,
  96.     0xFF, 0x3F, 0x01, 'C', 'M', 'C', 0,
  97.     0xFF, 0x2F, 0x01, 'C', 'M', 'A', 0,
  98.     0xFF, 0xEB, 0x01, 'X', 'C', 'H', 'G', 0,
  99.     0xFF, 0xE3, 0x01, 'X', 'T', 'H', 'L', 0,
  100.     0xFF, 0xF9, 0x01, 'S', 'P', 'H', 'L', 0,
  101.     0xFF, 0xE9, 0x01, 'P', 'C', 'H', 'L', 0,
  102.     0xFF, 0xDB, 0x02, 'I', 'N', ' ', 0,
  103.     0xFF, 0xD3, 0x02, 'O', 'U', 'T', ' ', 0,
  104.     0xFF, 0x07, 0x01, 'R', 'L', 'C', 0,
  105.     0xFF, 0x0F, 0x01, 'R', 'R', 'C', 0,
  106.     0xFF, 0x17, 0x01, 'R', 'A', 'L', 0,
  107.     0xFF, 0x1F, 0x01, 'R', 'A', 'R', 0,
  108.     0xFF, 0xC6, 0x02, 'A', 'D', 'I', ' ', 0,
  109.     0xFF, 0xCE, 0x02, 'A', 'C', 'I', ' ', 0,
  110.     0xFF, 0xD6, 0x02, 'S', 'U', 'I', ' ', 0,
  111.     0xFF, 0xDE, 0x02, 'S', 'B', 'I', ' ', 0,
  112.     0xFF, 0xE6, 0x02, 'A', 'N', 'I', ' ', 0,
  113.     0xFF, 0xF6, 0x02, 'O', 'R', 'I', ' ', 0,
  114.     0xFF, 0xEE, 0x02, 'X', 'R', 'I', ' ', 0,
  115.     0xFF, 0x00, 0x01, 'N', 'O', 'P', 0,
  116.     /*  8085 specific instructions */
  117.     0xFF, 0x20, 0x01, 'R', 'I', 'M', 0,
  118.     0xFF, 0x30, 0x01, 'S', 'I', 'M', 0,
  119.     /*  Jumps, Calls & Returns */
  120.     0xFF, 0xC3, 0x0B, 'J', 'M', 'P', ' ', 0,
  121.     0xFF, 0xCA, 0x43, 'J', 'Z', ' ', 0,
  122.     0xFF, 0xC2, 0x4B, 'J', 'N', 'Z', ' ', 0,
  123.     0xFF, 0xDA, 0x13, 'J', 'C', ' ', 0,
  124.     0xFF, 0xD2, 0x1B, 'J', 'N', 'C', ' ', 0,
  125.     0xFF, 0xEA, 0x23, 'J', 'P', 'E', ' ', 0,
  126.     0xFF, 0xE2, 0x2B, 'J', 'P', 'O', ' ', 0,
  127.     0xFF, 0xFA, 0x83, 'J', 'M', ' ', 0,
  128.     0xFF, 0xF2, 0x8B, 'J', 'P', ' ', 0,
  129.     0xFF, 0xCD, 0x0B, 'C', 'A', 'L', 'L', ' ', 0,
  130.     0xFF, 0xCC, 0x43, 'C', 'Z', ' ', 0,
  131.     0xFF, 0xC4, 0x4B, 'C', 'N', 'Z', ' ', 0,
  132.     0xFF, 0xDC, 0x13, 'C', 'C', ' ', 0,
  133.     0xFF, 0xD4, 0x1B, 'C', 'N', 'C', ' ', 0,
  134.     0xFF, 0xEC, 0x23, 'C', 'P', 'E', ' ', 0,
  135.     0xFF, 0xE4, 0x2B, 'C', 'P', 'O', ' ', 0,
  136.     0xFF, 0xFC, 0x83, 'C', 'M', ' ', 0,
  137.     0xFF, 0xF4, 0x8B, 'C', 'P', ' ', 0,
  138.     0xFF, 0xC9, 0x05, 'R', 'E', 'T', 0,
  139.     0xFF, 0xC8, 0x45, 'R', 'Z', 0,
  140.     0xFF, 0xC0, 0x4D, 'R', 'N', 'Z', 0,
  141.     0xFF, 0xD8, 0x15, 'R', 'C', 0,
  142.     0xFF, 0xD0, 0x1D, 'R', 'N', 'C', 0,
  143.     0xFF, 0xE8, 0x25, 'R', 'P', 'E', 0,
  144.     0xFF, 0xE0, 0x2D, 'R', 'P', 'O', 0,
  145.     0xFF, 0xF8, 0x85, 'R', 'M', 0,
  146.     0xFF, 0xF0, 0x8D, 'R', 'P', 0,
  147.     /*  Register based instructions */
  148.     0xC0, 0x40, 0x01, 'M', 'O', 'V', ' ', 'd', ',', 's', 0,
  149.     0xC7, 0x06, 0x02, 'M', 'V', 'I', ' ', 'd', ',', 0,
  150.     0xF8, 0x90, 0x01, 'S', 'U', 'B', ' ', 's', 0,
  151.     0xF8, 0x98, 0x01, 'S', 'B', 'B', ' ', 's', 0,
  152.     0xF8, 0x80, 0x01, 'A', 'D', 'D', ' ', 's', 0,
  153.     0xF8, 0x88, 0x01, 'A', 'D', 'C', ' ', 's', 0,
  154.     0xF8, 0xA0, 0x01, 'A', 'N', 'A', ' ', 's', 0,
  155.     0xF8, 0xB0, 0x01, 'O', 'R', 'A', ' ', 's', 0,
  156.     0xF8, 0xA8, 0x01, 'X', 'R', 'A', ' ', 's', 0,
  157.     0xF8, 0xB8, 0x01, 'C', 'M', 'P', ' ', 's', 0,
  158.     0xC7, 0x04, 0x01, 'I', 'N', 'R', ' ', 'd', 0,
  159.     0xC7, 0x05, 0x01, 'D', 'C', 'R', ' ', 'd', 0,
  160.     /*  Register pair instructions */
  161.     0xCF, 0x01, 0x03, 'L', 'X', 'I', ' ', 'p', ',', 0,
  162.     0xEF, 0x0A, 0x01, 'L', 'D', 'A', 'X', ' ', 'p', 0,
  163.     0xEF, 0x02, 0x01, 'S', 'T', 'A', 'X', ' ', 'p', 0,
  164.     0xCF, 0x03, 0x01, 'I', 'N', 'X', ' ', 'p', 0,
  165.     0xCF, 0x0B, 0x01, 'D', 'C', 'X', ' ', 'p', 0,
  166.     0xCF, 0x09, 0x01, 'D', 'A', 'D', ' ', 'p', 0,
  167.     0xCF, 0xC5, 0x01, 'P', 'U', 'S', 'H', ' ', 'p', 0,
  168.     0xCF, 0xC1, 0x01, 'P', 'O', 'P', ' ', 'p', 0,
  169.     /*  Restart instruction */
  170.     0xC7, 0xC7, 0x01, 'R', 'S', 'T', ' ', 'v', 0,
  171.     /*  This entry always matches invalid opcodes */
  172.     0x00, 0x00, 0x01, 'D', 'B', ' ', 0 }
  173.  
  174. /*
  175.  * Tables to convert register index into actual names
  176.  */
  177. char regtab[]    = { 'B','C','D','E','H','L','M','A' };
  178. char rptab[]    = { 'B','D','H','S' };
  179.  
  180. /*
  181.  * Symbol output flags
  182.  */
  183. char *flags[] = { "?", "B", "W", "B&W" };
  184.  
  185. /*
  186.  * Symbol table variables
  187.  */
  188. char sname[SYMBOLS][9], sflags[SYMBOLS];
  189. unsigned svalue[SYMBOLS], scount = 0;
  190.  
  191. /*
  192.  * Buffer for reading file, and recording opcode data
  193.  */
  194. unsigned char memory[64];
  195.  
  196. /*
  197.  * Command option flags
  198.  */
  199. unsigned int begin = 0, size = -1, addr = 0;
  200. char aflag = -1, bflag = 0, dflag = 0, eflag = -1;
  201. FILE *dfp = 0;
  202.  
  203. /*
  204.  * Perform a disassembly
  205.  */
  206. main(argc, argv)
  207.     int argc;
  208.     char *argv[];
  209. {
  210.     FILE *fp;
  211.     unsigned a, i, j, length, s;
  212.     unsigned char *ptr, opcode, c;
  213.  
  214.     if(argc < 2)
  215. abort("\n8085 Cross Disassembler\n\n\
  216. Use: dis85 <filename> [options ...]\n\n\
  217. Options: -a     - Assembly output\n\
  218.          -b     - include Byte constants (in -d)\n\
  219.          -d     - pre-Define symbols for addresses\n\
  220.          -e     - Exclude out of range addresses (in d=)\n\
  221.          a=addr - specify memory Address\n\
  222.          b=size - specify Beginning offset (in file)\n\
  223.          d=file - Dum